<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { equipApi } from '@/api/controller';
  import { ToastWarning, Toastloading } from '@/utils/public';
  import { getPlayEndTime, getDateArray, getHMMSS } from '@/utils'
  import statPoolItemDetail from "./statPoolItemDetail.vue";
  import Pagination from "@/components/Pagination/index.vue";
  const statPoolItemDetailRef = ref()
  const action = reactive({
    dialogFormVisible:false,
    logsList:[] as Array<any>,
    gameList:[] as Array<any>,
    list:[] as any[],
    pool_one_list:[] as Array<any>,
    total:0,
    dateArr:getDateArray('time') as Array<number>
  })
  const listQuery = reactive({
    game_id:'',
    page:1,
    limit:20,
    pool_one_id:''
  })
  const resetQuery = ()=>{
    Object.assign(listQuery,{
      game_id:'',
      page:1,
      limit:20,
      pool_one_id:''
    })
    getList()
  }
  const getGameIdList = async()=>{
    const res = await equipApi.gameSwitch()
    const { data=[] } = res || {}
    action.gameList = data
    listQuery.pool_one_id = ''
    action.pool_one_list = []
  }
  const getPool_One_list = async()=>{
    const loading = Toastloading()
    const res = await equipApi.poolOneList({
      game_id:listQuery.game_id
    })
    loading.close()
    const { data=[] } = res || {}
    action.pool_one_list = data
  }

  const handleDetail = (row:any = {})=>{
    const { pool_one_id=0 } = row
    if(!pool_one_id) return ToastWarning('pool_one_id error')
    const { playTime:date_st = '', endTime:date_ed='' } = getPlayEndTime(action.dateArr)
    statPoolItemDetailRef.value.openDialog({
      pool_one_id,
      date_st,
      date_ed
    })
  }
  const handleSerach = ()=>{
    listQuery.page = 1
    getList()
  }
  const getList = async()=> {
    const loading = Toastloading()
    const { playTime:date_st = '', endTime:date_ed='' } = getPlayEndTime(action.dateArr)
    const res = await equipApi.statPoolItem({
      ...listQuery,
      date_st,
      date_ed
    })
    loading.close()
    const { data=[],total=0 } = res.data || {}
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getGameIdList()
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">游戏ID：</div>
          <el-select v-model="listQuery.game_id" @change="getPool_One_list" clearable placeholder="请选择游戏ID">
            <el-option v-for="item in action.gameList" :label="item.game_name" :value="item.game_id" />
          </el-select>
        </div>
        <div class="labelItem">
          <div class="label">一级礼物池：</div>
          <el-select v-model="listQuery.pool_one_id" clearable placeholder="请选择一级礼物池">
            <el-option
              v-for="item in action.pool_one_list"
              :label="item.item_name"
              :value="item.id" />
          </el-select>
        </div>
        <div class="labelItem">
          <div class="label">时间：</div>
          <el-date-picker
            v-model="action.dateArr"
            type="datetimerange"
            format="YYYY-MM-DD HH:mm:ss "
            value-format="x"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="resetQuery">重置</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-TableCenter"
      :data="action.list"
      border>
      <el-table-column min-width="120" prop="game_id" label="游戏ID" />
      <el-table-column min-width="120" prop="game_name" label="游戏名称" />
      <el-table-column min-width="120" prop="pool_one_id" label="一级礼物池名称" />
      <el-table-column min-width="120" prop="pool_one_name" label="一级礼物池名称" />
      <el-table-column min-width="120" prop="itemNum" label="数量" />
      <el-table-column min-width="120" prop="rate" label="当前概率">
        <template #default="scope">{{ scope.row.rate }}%</template>
      </el-table-column>
      <el-table-column min-width="120" prop="reward_jd_num" label="产出价值金豆数" />
      <el-table-column min-width="120" label="奖励产出详情">
        <template #default="scope">
          <el-button type="primary" @click="handleDetail(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
      <statPoolItemDetail ref="statPoolItemDetailRef" />
  </div>
</template>
